package com.yoomiito.app.ui.anyview.order;

import android.app.Activity;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.address.AddressListActivity;
import com.yoomiito.app.address.EditAddressActivity;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.AddressInfo;
import com.yoomiito.app.utils.Const;
import com.yoomiito.app.utils.DensityUtil;

/**
 * 用于显示地址
 */
public class ShowAddressView extends FrameLayout {

    private View mAddressView;
    private View mAddressEmptyView;
    private AddressInfo mAddressInfo;
    private TextView mNameTv;
    private TextView mTelTv;
    private TextView mAddressTv;

    public ShowAddressView(@NonNull Context context) {
        super(context);
        mAddressView = LayoutInflater.from(context).inflate(R.layout.item_address, null, false);
        mNameTv = mAddressView.findViewById(R.id.item_address_name);
        mTelTv = mAddressView.findViewById(R.id.item_address_tel);
        mAddressTv = mAddressView.findViewById(R.id.item_address_text);
        mAddressEmptyView = LayoutInflater.from(context).inflate(R.layout.item_address_empty, null, false);
        OnClickListener onClickListener = v -> AddressListActivity.Companion.startForResult((BaseActivity) context, mAddressInfo, Const.RequestCode.AddressCode);
        mAddressEmptyView.setOnClickListener(v -> {
            EditAddressActivity.start((Activity) context,null,Const.RequestCode.AddressCode);
        });
        mAddressView.setOnClickListener(onClickListener);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(90));
        addView(mAddressView,lp);
        addView(mAddressEmptyView,lp);
    }

    public void showEmpty() {

    }

    public void showAddress() {
        if (mAddressEmptyView.getVisibility() == VISIBLE)
            mAddressEmptyView.setVisibility(GONE);
        mAddressView.setVisibility(VISIBLE);
    }

    public void setAddressInfo(AddressInfo addressInfo) {
        if (addressInfo==null||addressInfo.getId()==null)return;
        mAddressInfo = addressInfo;
        mNameTv.setText(mAddressInfo.getUserName());
        mTelTv.setText(mAddressInfo.getMobile());
        mAddressTv.setText(mAddressInfo.getProvinceName() + mAddressInfo.getCityName() + mAddressInfo.getCountyName() +
                mAddressInfo.getAddress());
    }

    public AddressInfo getmAddressInfo() {
        return mAddressInfo;
    }
}
